package com.yingpai.fitness.dvp_2_0.main.activity.mine.integral;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lzy.okgo.model.Response;
import com.yingpai.fitness.R;
import com.yingpai.fitness.dvp_2_0.common.activity.CommonActivity;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.callback.Callback;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.request.RequestType;
import com.yingpai.fitness.dvp_2_0.common.util.constant.Contacts;
import com.yingpai.fitness.dvp_2_0.main.bean.CouponDetailBean;
import com.yingpai.fitness.dvp_2_0.main.bean.IsSuccessBean;
import com.yingpai.fitness.dvp_2_0.main.util.DialogUtil;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Constant;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Url;

import org.greenrobot.eventbus.EventBus;

import java.text.DecimalFormat;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by ASUS on 2018/12/19.
 */

public class IntegralMallCouponActivity extends CommonActivity {
    @BindView(R.id.right_tv)
    TextView rightTv;
    @BindView(R.id.money)
    TextView money;
    @BindView(R.id.avaliable)
    TextView avaliable;
    @BindView(R.id.title_tv)
    TextView title_tv;
    @BindView(R.id.integral)
    TextView integral;
    @BindView(R.id.content)
    TextView content;
    @BindView(R.id.icon)
    CircleImageView icon;
    private String id;
    private CouponDetailBean mCouponDetailBean;
    private DecimalFormat df = new DecimalFormat("######.###");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStatusFlag(Contacts.NORMAL_STATUS);
        setContentView(R.layout.activity_integral_mall_coupon);
    }

    @Override
    protected void initView() {
        setTitle("商品详情");
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            id = bundle.getString("id");
        }
//        Drawable drawableLeft = getResources().getDrawable(
//                R.mipmap.share);
//        rightTv.setCompoundDrawablesWithIntrinsicBounds(drawableLeft,
//                null, null, null);
    }

    @Override
    protected void initData() {
        showDialog(this,null);
        HashMap<String,Object> params = new HashMap();
        params.put("id",id);
        finalOkGo.requestEncrypt(RequestType.POSTENCRYPT, Contacts.HOST_SERVER + Url.INTEGRAL_MALL_COUPON_DETAIL, params, Url.INTEGRAL_MALL_COUPON_DETAIL, new Callback<CouponDetailBean>() {
            @Override
            public void onSuccess(CouponDetailBean couponDetailBean) {
                if(couponDetailBean.getCode().equals(Constant.REQUEST_SUCCESS_0)){
                    mCouponDetailBean = couponDetailBean;
                    money.setText("¥"+df.format(couponDetailBean.getData().getMinus()/100));
                    avaliable.setText("消费满"+df.format(couponDetailBean.getData().getFull()/100)+"元可用");
                    title_tv.setText(couponDetailBean.getData().getName());
                    integral.setText(couponDetailBean.getData().getExchangePoint()+"");
                    content.setText(couponDetailBean.getData().getTicketInfo().getTitle() + "\n" + couponDetailBean.getData().getTicketInfo().getInfo());
                    Glide.with(context)
                            .load(couponDetailBean.getData().getPhotoUrl())
                            .error(R.mipmap.coupon_icon)
                            .into(icon);
                }else{
                    showToast(couponDetailBean.getMessage()+"");
                }
                dismissDialog();
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                dismissDialog();
            }
        });
    }

    @Override
    @OnClick({R.id.right_tv, R.id.bottom_tv})
    public void onViewClicked(View view) {
        super.onViewClicked(view);
        switch (view.getId()) {
            case R.id.right_tv:
                break;
            case R.id.bottom_tv:
                DialogUtil.getInstance().showCenterDialog(this, R.layout.dialog_shop_pay_success);
                View dialog_logout = DialogUtil.getInstance().dialog_center_layout;
                dialog_logout.findViewById(R.id.close).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        DialogUtil.getInstance().dialog_center.dismiss();
                    }
                });
                TextView title = dialog_logout.findViewById(R.id.title);
                title.setText("确定使用"+mCouponDetailBean.getData().getExchangePoint()+"积分兑换？");
                dialog_logout.findViewById(R.id.message).setVisibility(View.GONE);
                dialog_logout.findViewById(R.id.sure_cancel).setVisibility(View.VISIBLE);
                TextView sure = dialog_logout.findViewById(R.id.sure);
                TextView cancel = dialog_logout.findViewById(R.id.cancel);
                cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        DialogUtil.getInstance().dialog_center.dismiss();
                    }
                });
                sure.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        DialogUtil.getInstance().dialog_center.dismiss();
                        showDialog(IntegralMallCouponActivity.this,null);
                        HashMap<String,Object> map = new HashMap<>();
                        map.put("id",mCouponDetailBean.getData().getId());
                        finalOkGo.requestEncrypt(RequestType.POSTENCRYPT, Contacts.HOST_SERVER + Url.EXCHANGE_COUPON, map, Url.EXCHANGE_COUPON, new Callback<IsSuccessBean>() {
                            @Override
                            public void onSuccess(IsSuccessBean isSuccessBean) {
                                dismissDialog();
                                if(isSuccessBean.getCode().equals(Constant.REQUEST_SUCCESS_0)){
                                    showToast("兑换成功");
                                    EventBus.getDefault().post("==exchange==");
                                    finishActivity();
                                }else{
                                    showToast(isSuccessBean.getMessage()+"");
                                }
                            }

                            @Override
                            public void onError(Response<String> response) {
                                super.onError(response);
                                dismissDialog();
                            }
                        });
                    }
                });
                break;
        }
    }
}
